package Data;

import java.util.ArrayList;
import java.util.Collections;

public class UserGroup {

	private String groupName;
	private String newName;
	private ArrayList<User> groupUsers;

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}

	public ArrayList<User> getGroupUsers() {
		return groupUsers;
	}

	public UserGroup(String groupName, ArrayList<User> groupUsers) {
		this.groupName = groupName;
		this.newName = groupName;
		this.groupUsers = groupUsers;
	}

	public UserGroup(String groupName) {
		this(groupName, new ArrayList<User>());
	}

	public boolean isNameChanged() {
		return newName.equals(groupName);
	}

	public User getTheUser(int index) {
		return groupUsers.get(index);
	}

	public void addUser(User newUser) {
		groupUsers.add(newUser);
		sortUsers();
	}

	public void deleteUser(User newUser) {
		int size = groupUsers.size();
		for (int i = 0; i < size; i++) {
			if (groupUsers.get(i).getIp().equals(newUser.getIp())) {
				groupUsers.remove(i);
				break;
			}
		}
	}
	public void sortUsers() {
		Collections.sort(groupUsers,new UserComparators());
	}
	
	/**
	 * get user from the array for the right ip address
	 * @param ip
	 * @return the user
	 */
	public User getUser(String ip){
		User user = null;
		for(int loopi = 0;loopi<groupUsers.size();loopi++){
			if(groupUsers.get(loopi).getIp() == ip)
				user = groupUsers.get(loopi);
		}
		
		return user;
	}
}
